package com.lijie.bs_recruit.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lijie.bs_recruit.Util.WrapperUtil;
import com.lijie.bs_recruit.mapper.CompanyMapper;
import com.lijie.bs_recruit.mapper.PositionMapper;
import com.lijie.bs_recruit.model.Company;
import com.lijie.bs_recruit.model.Position;
import com.lijie.bs_recruit.service.PositionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  职位Service
 * </p>
 *
 * @author 李杰
 * @since 2021-03-15
 */
@Service
public class PositionServiceImpl extends ServiceImpl<PositionMapper, Position> implements PositionService {
    @Autowired
    PositionMapper positionMapper;
    @Autowired
    CompanyMapper companyMapper;

    @Autowired
    WrapperUtil wrapperUtil;



//    /**
//     * 复合查询：查询审核或删除 分页
//     * @param isCheck 审核  0-未审核；1-已审核
//     * @param isDeleted 删除  0-未删除；1-已删除
//     * @return
//     */
//    public PageInfo<Position> selCheckedPositionPage(Integer isCheck, Integer isDeleted, Integer pageIndex) {
//        Page<Position> page=new Page<>(pageIndex,10);
//        QueryWrapper<Position> wrapper =new QueryWrapper<>();
//        if(isCheck!=null){
//            wrapper.eq("ischeck",isCheck);
//        }else if(isDeleted!=null){
//            wrapper.eq("ischeck",isDeleted);
//        }
//        Page<Position> positionPage = positionMapper.selectPage(page, wrapper);
//        return positionPage;
//    }

    //多条件分页查询
    @Override
    public PageInfo<Position> selPositionPage(Integer pageIndex, Position position) {
        if(pageIndex == null) pageIndex = 1;
        QueryWrapper<Position> positionQueryWrapper = wrapperUtil.wrapperPosition(position);
        PageHelper.startPage(pageIndex,6);
        List<Position> positions = positionMapper.selectList(positionQueryWrapper);
        for(Position p:positions){
            Company company = companyMapper.selectById(p.getCompanyId());
            p.setCompany(company);
        }
        PageInfo<Position> pageInfo = new PageInfo<>(positions);
        return pageInfo;
    }

    @Override
    public Boolean updatePositionCheck(Position position) {
        UpdateWrapper<Position> wrapper=new UpdateWrapper<>();
        wrapper.eq("positionId", position.getPositionId())
                .set("ischeck" ,position.getIscheck() );
        int update = baseMapper.update(position, wrapper);
        return update>0;
    }

    @Override
    public PageInfo<Position> adminSelPositionChecked(Integer pageIndex, Position position) {
        if(pageIndex == null) pageIndex = 1;
        QueryWrapper<Position> positionQueryWrapper = wrapperUtil.wrapperPosition(position);
        positionQueryWrapper.ne("ischeck",0);
        PageHelper.startPage(pageIndex,6);
        List<Position> positions = positionMapper.selectList(positionQueryWrapper);
        PageInfo<Position> pageInfo = new PageInfo<>(positions);
        return pageInfo;
    }

    /**
     * 管理员更新审核状态
     * @param PositionId
     * @return
     */
//    @Override
//    public Boolean updatePositionCheck(Integer PositionId ) {
//        Position position = positionMapper.selectById(PositionId);
//        position.setIscheck(position.getIscheck()==0?1:0);
//        int i = positionMapper.updateById(position);
//        return i==1;
//    }

    @Override
    public Boolean updatePositionOpen(Integer PositionId) {
        Position position = positionMapper.selectById(PositionId);
        position.setIsOpen(position.getIsOpen()==0?1:0);
        int i = positionMapper.updateById(position);
        return i==1;
    }

    @Override
    public Position selPosotionByid(Integer PositionId){
        return positionMapper.selectById(PositionId);
    }

    //通过
    @Override
    public PageInfo<Position> selPositionByWrapper(Integer pageIndex, QueryWrapper<Position> wrapper) {
        PageHelper.startPage(pageIndex,6);
        PageInfo<Position> pageInfo = new PageInfo<>(positionMapper.selectList(wrapper));
        return pageInfo;
    }
}
